<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="订单号:" prop="order_id.value">
                            <el-input type="textarea" :rows="4" v-model="queryParams.order_id.value" placeholder="请输入订单号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="SKU:" prop="sku.value">
                            <el-input v-model="queryParams.sku.value" placeholder="请输入SKU" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="跟踪号:" prop="tracking_number.value">
                            <el-input v-model="queryParams.tracking_number.value" placeholder="请输入跟踪号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="姓名:" prop="name.value">
                            <el-input v-model="queryParams.name.value" placeholder="请输入姓名" clearable />
                        </el-form-item>
                    </el-col>
                
                    <el-col :md="6" :sm="12">
                        <el-form-item label="创建时间:" prop="paid_time.value">
                            <el-date-picker
                                v-model="queryParams.paid_time.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col> -->
                
                <el-col :span="1.5">
                    <!-- <el-form-item label="海外仓" prop=""> -->
                        <el-select
                            v-model="overseas_warehouse"
                            clearable
                            placeholder="请选择海外仓">
                                <el-option
                                    v-for="item in overseas_warehouse_arr"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    <span style="float: left">{{ item.label }}</span>
                                    <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span> -->
                                </el-option>
                        </el-select>
                    <!-- </el-form-item> -->
                </el-col>
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleModification" v-hasPermi="api.automatic_shipping">自动发货</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-plus" size="mini" @click="handleprocessed" v-hasPermi="api.processed">暂无渠道发货</el-button>
                </el-col>

                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>
            <el-row>
                <el-col :span="1.5">
                    <el-upload
                        class="upload-demo"
                        ref="upload"
                        :action="api.upload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="handleUploadSuccess"
                        :on-error="handleUploadError"
                        :file-list="fileList"
                        :auto-upload="false">
                        <el-button slot="trigger" size="small" type="primary">选择文件</el-button>
                        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传网红订单</el-button>
                        <el-button size="small" type="warning" >
                            <el-link style="color: #ffffff;" href="http://tiktokapi.yilebuy.com/storage/system/orderwh.xls">下载网红订单</el-link>
                        </el-button>
                    </el-upload>
                </el-col>
            </el-row>

            <!-- 列表 -->
            <el-tabs v-model="activeTab" @tab-click="handleStatus" type="border-card">
                <el-tab-pane v-for="(item, index) in status" :key="index" :label="item.label" :name="item.value.toString()">
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="" align="left">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button>
                            <!-- <el-button class="btn-mar" size="mini" type="info" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="danger"  @click="handledelivery_again(scope.row)" v-hasPermi="api.delivery_again">重新发货</el-button> -->
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            </el-tab-pane>
            </el-tabs>

            <el-dialog :title="title1" :visible.sync="dialogTableVisible">
                <el-table :data="msgdata">
                    <el-table-column property="msg" label="日志" width=""></el-table-column>
                </el-table>
            </el-dialog>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    
    data() {
        return {
            fileList: [],
            cancel_reasons: [],
            overseas_warehouse_arr: [
                // {
                //     value: '1',
                //     label: 'JJS'
                // },
                // {
                //     value: '2',
                //     label: '全球无忧美国TK海外仓'
                // },
                {
                    value: '3',
                    label: '金蚁'
                },
                {
                    value: '4',
                    label: '顺丰'
                }
            ],
            overseas_warehouse: '3',
            dialogTableVisible: false,
            title1: '请等待',
            // table结构
            columns: [
                {
                    visible: true,
                    label: '订单号',
                    prop: 'order_id',
                },
                {
                    visible: true,
                    label: '创建时间',
                    sortable: 'custom',
                    prop: 'createtime',
                },
                {
                    visible: true,
                    label: '发货状态',
                    prop: 'tracking_name',
                },
                {
                    visible: true,
                    label: '跟踪号',
                    prop: 'tracking_number',
                },
                {
                    visible: true,
                    label: '仓库',
                    prop: 'warehouse_name',
                },
                {
                    visible: true,
                    label: '发货详情',
                    prop: 'tracking_msg',
                },
                {
                    visible: true,
                    label: '达人姓名',
                    prop: 'name',
                },
                // {
                //     visible: true,
                //     label: '链接',
                //     prop: 'link',
                // },
                {
                    visible: true,
                    label: '发货SKU',
                    prop: 'skuinfo',
                },
                // {
                //     visible: true,
                //     label: '邮箱',
                //     prop: 'buyer_email',
                // },
                // {
                //     visible: true,
                //     label: '电话',
                //     prop: 'phone_number',
                // },
                // {
                //     visible: true,
                //     label: '详细地址',
                //     prop: 'address_detail',
                // },
                // {
                //     visible: true,
                //     label: '邮编',
                //     prop: 'postal_code',
                // },
                // {
                //     visible: true,
                //     label: '省份',
                //     prop: 'state',
                // },
                // {
                //     visible: true,
                //     label: '市区',
                //     prop: 'city',
                // },
                // {
                //     visible: true,
                //     label: '县',
                //     prop: 'county',
                // },
                // {
                //     visible: true,
                //     label: '备注',
                //     prop: 'remarks',
                // },
                // {
                //     visible: true,
                //     label: '邮编',
                //     prop: 'postal_code',
                // },
            ],
            // 接口地址
            api: {
                index: `/admin/orderwh/index`,
                find: `/admin/orderwh/find`,
                add: `/admin/orderwh/add`,
                edit: `/admin/orderwh/edit`,
                delete: `/admin/orderwh/delete`,
                export: `/admin/orderwh/export`,
                processed: `/admin/orderwh/processed`,
                upload: `https://tiktokapi.yilebuy.com/admin/orderwh/uploads`,
                automatic_shipping: `/admin/orderwh/automatic_shipping`,
                delivery_again: `/admin/orderwh/delivery_again`,
                status: `/admin/orderwh/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'createtime',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                order_id: { value:undefined, op: '%like%'},
                sku: { value:undefined, op: '%like%'},
                tracking_number: { value:undefined, op: '%like%'},
                name: { value:undefined, op: '%like%'},
                fulfillment_type: { value:undefined, op: '%like%'},
                paid_time: { value:[], op: 'range'},
                tracking_beforehand: { value:[], op: '='},
                site_id: { value:[], op: '='},
                cancel_reason: { value:[], op: '='},
            },
            status: [
                { label: '全部', value: '' },
                { label: '等待发货', value: '10' },
                { label: '排队发货中', value: '1' },
                { label: '发货异常', value: '2' },
                { label: '等待海外仓跟踪号', value: '3' },
                // { label: '发货成功', value: '4' },
                { label: '完成', value: '5' },
                { label: '暂无渠道发货', value: '6' },
            ],
            activeTab: '',
            msgdata:[],

        }
    },
    created () {
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        handleprocessed(row) {
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            this.$confirm(`是否确认转移到暂无渠道发货?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.processeds()
                })
                .catch(() => {})
        },

        async processeds() {
            await this.request.get(this.api.processed, { params: { id:this.ids } })
            this.initIndex()
            this.msgSuccess('操作成功')
        },
        // 再次发货
        handledelivery_again (row) {
            if(this.overseas_warehouse == ''){
                return this.msgError('请选择仓库再操作')
            }
            let tableIds = row[this.primaryKey] || this.ids
            tableIds = tableIds.toString()
            this.$confirm(`已选择仓库${this.overseas_warehouse},是否确认重新发货?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.dialogTableVisible = true;
                this.msgdata = [];
                this.title1 = '请等待';
                this.delivery_again2(tableIds,this.overseas_warehouse)
            }).then(() => {
                // this.initIndex()
                // this.msgSuccess('操作成功')
            }).catch(() => { })
        },
        async delivery_again2(tableIds,overseas_warehouse) {
            let response = await this.request.get(this.api.delivery_again, { params: { id:tableIds,overseas_warehouse:overseas_warehouse } })
            this.msgdata = response.data;
            this.title1 = '操作成功';
            this.initIndex()
        },

        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },

        submitUpload() {
            this.loading = true
            this.$refs.upload.submit();
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        
        // 上传成功回调
        handleUploadSuccess(res) {
            this.loading = false
            if (res.code == 1) {
                this.$message.success('上传成功');
                this.initIndex();
            } else {
                this.$message({
                    type: 'error',
                    message: '上传失败'+res.msg,
                })
            }
        },
        // 上传失败
        handleUploadError() {
            this.$message({
                type: 'error',
                message: '上传失败',
            })
        },
        
        //     // 详情
        // handleDetail(row) {
        //     this.handleFindDetail(row)
        // },

        // async handleFindDetail(row) {
        //     let response = await this.request.get(this.api.find, { params: {order_id:row.order_id}})
        //     this.findFormData = this.formatFindData(response.data)
        //     this.editOpen = true
        // },
          //订单状态切换
        handleStatus(tab) {
            this.queryParams.tracking_beforehand.value = tab.name
            this.submitSearch()
        },

        handleModification(row) {
            // console.log('this.ids.length',this.ids.length);
            // if(this.ids.length>10){
            //     return this.msgError('发货一次不超过10条')
            // }
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            if(this.overseas_warehouse == ''){
                return this.msgError('请选择仓库再操作')
            }
            this.$confirm(`是否确认自动发货，过程大概需要一分钟?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.loading = true
                    // this.dialogTableVisible = true;
                    // this.msgdata = [];
                    // this.title1 = '请等待';
                    // test
                    this.automatic_shippings()
                })
                .catch(() => {})
        },

        async automatic_shippings() {
            let response = await this.request.get(this.api.automatic_shipping, { params: { id:this.ids ,warehouse:this.overseas_warehouse} })
            // this.msgdata = response.data;
            // this.title1 = '操作成功';
            this.initIndex()
            this.msgSuccess('操作成功，请在发货日志中查看明细')
            this.loading = false
        },
        
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
